const OSS = require('ali-oss');
const express = require('express');
const path = require('path');
const config = require('../config/oss');
const request = require('request');
const bent = require('bent');
const app = express();
const port = 7000;
const fs = require('fs');

app.use(express.static(path.join(__dirname, '../../public')));

let client = new OSS(config);
// const url = 'http://localhost:4001/utils/tools.js';
const url = 'http://localhost:4001/aa.zip';

const getInfo = async function (key) {
  const result = await client.get(key);
  // console.log(result);
  const res = result.res;
  const len = res.size;
  const url = res.requestUrls;
  const type = res.headers['content-type'];
  console.log(`len = ${len}`);
  console.log(`type = ${type}`);
  console.log(`url = ${url}`);
  return {
    len,
    type,
    key
  }
};

const getStream = function (url) {
  return bent(url)();
};

/**
 * 保存文件到oss
 */
const saveToOSS = async function (key, url) {
  try{
    console.time('getStream');
    let stream = await getStream(url);
    console.timeEnd('getStream');
    // console.log(stream);
    const write = fs.createWriteStream('aa.zip');
    stream.pipe(write);
    // return client.putStream(key, stream);

    const write2 = fs.createWriteStream('bb.zip');
    stream.pipe(write2);
  }catch (e){
    console.error(e.message);
    console.timeEnd('getStream');
  }
};

const saveChromeDriver = async function () {
  console.time('test');
  await saveToOSS('chromedriver_linux64.zip', 'https://cdn.npm.taobao.org/dist/chromedriver/2.46/chromedriver_linux64.zip');
  console.timeEnd('test');
};

// (async function () {
//   // object表示上传到OSS的Object名称，localfile表示本地文件或者文件路径
//   // await client.put('example.png', '../image/example.png');
//   // console.log(request(url));
//   // await client.putStream('tools.js', request(url));
//   // const res = await client.get('example.png');
//   // console.log(res);
//
//   // save('test.js', url + 'abc');
//   // save('test.js', url);
//   // console.log('上传完毕');
//   // await getInfo('tools.js');
//   const res = await getInfo('chromedriver_linux64.zip');
//   console.log(res);
//   // const a = request(url + 'abc');
//   // debugger;
//   // console.log(await request(url + 'abc'));
//   // console.log(await request(url));
// })();

/**
 * 获取阿里云地址
 */
const getOSSUrl = function (key) {
  let url = client.signatureUrl(key, {expires: 3600});
  console.log(url);
  return url;
};

app.use('/origin', async function (req, res, next) {
  res.type('application/octet-stream');
  // res.attachment('abc.txt');
  res.attachment('abc.zip');
  // req.pipe(request(url)).pipe(res);
  const stream = await getStream(url);
  // req.pipe(await stream).pipe(res);
  stream.pipe(res);

  const write = fs.createWriteStream('aa.zip');
  stream.pipe(write);
});

app.post('/download', function (req, res) {
  console.log('------down');
  res.type('application/octet-stream');
  res.attachment('abc.txt');
  // res.append('Access-Control-Allow-Origin', '*');
  req.pipe(request.get(url)).pipe(res);
});

app.use('/oss', async function (req, res, next) {
  res.send(getOSSUrl('test.js'));
});

app.listen(port, function () {
  // getOSSUrl('test.js');
  console.info(`server started in http://localhost:${port}`);
});
// saveToOSS('abc/aa.js', url);
